package com.example.bookmanagersystem.controller;

import com.example.bookmanagersystem.entity.Reader;
import com.example.bookmanagersystem.service.ReaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping("/readers")
public class ReaderController {
    @Autowired
    private ReaderService readerService;

    // 读者列表
    @GetMapping
    public String listReaders(Model model, @RequestParam(required = false) String keyword) {
        if (keyword != null && !keyword.isEmpty()) {
            model.addAttribute("readers", readerService.searchReadersByName(keyword));
        } else {
            model.addAttribute("readers", readerService.getAllReaders());
        }
        return "readers/list";
    }

    // 跳转到添加读者页面
    @GetMapping("/new")
    public String showAddForm(Model model) {
        model.addAttribute("reader", new Reader());
        model.addAttribute("title", "添加读者");
        return "readers/form";
    }

    // 保存读者
    @PostMapping
    public String saveReader(@ModelAttribute Reader reader, RedirectAttributes redirectAttributes) {
        try {
            readerService.addReader(reader);
            redirectAttributes.addFlashAttribute("message", "添加读者成功");
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("error", "添加失败：" + e.getMessage());
            return "redirect:/readers/new"; // 保存失败返回表单页
        }
        return "redirect:/readers";
    }

    // 跳转到编辑读者页面
    @GetMapping("/edit/{id}")
    public String showEditForm(@PathVariable Long id, Model model, RedirectAttributes redirectAttributes) {
        try {
            Reader reader = readerService.getReaderById(id)
                    .orElseThrow(() -> new RuntimeException("读者不存在"));
            model.addAttribute("reader", reader);
            model.addAttribute("title", "编辑读者");
            return "readers/form";
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("error", e.getMessage());
            return "redirect:/readers";
        }
    }

    // 更新读者
    @PostMapping("/update/{id}")
    public String updateReader(@PathVariable Long id, @ModelAttribute Reader reader, RedirectAttributes redirectAttributes) {
        try {
            reader.setId(id);
            readerService.updateReader(reader);
            redirectAttributes.addFlashAttribute("message", "更新读者成功");
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("error", "更新失败：" + e.getMessage());
            return "redirect:/readers/edit/" + id; // 更新失败返回编辑页
        }
        return "redirect:/readers";
    }

    // 删除读者
    @GetMapping("/delete/{id}")
    public String deleteReader(@PathVariable Long id, RedirectAttributes redirectAttributes) {
        try {
            readerService.deleteReader(id);
            redirectAttributes.addFlashAttribute("message", "删除读者成功");
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("error", "删除失败：" + e.getMessage());
        }
        return "redirect:/readers";
    }

    // 查看读者详情
    @GetMapping("/{id}")
    public String viewReader(@PathVariable Long id, Model model, RedirectAttributes redirectAttributes) {
        try {
            Reader reader = readerService.getReaderById(id)
                    .orElseThrow(() -> new RuntimeException("读者不存在"));
            model.addAttribute("reader", reader);
            return "readers/view";
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("error", e.getMessage());
            return "redirect:/readers";
        }
    }
}